package com.zjdiepu.www.partner.api.xinhehui;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.diepu.www.client.partner.xhh.enums.PartnerErrorCode;
import com.diepu.www.client.partner.xhh.enums.XeApplyState;
import com.diepu.www.client.partner.xhh.request.NoticeStatusRequest;
import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.ThirdLogEventStatus;
import com.zjdiepu.www.ibiz.partner.IThLendingService;
import com.zjdiepu.www.ibiz.partner.IThLogsService;
import com.zjdiepu.www.ibiz.partner.IThYwBusinessRequestService;
import com.zjdiepu.www.ibiz.partner.IThYwBusinessService;
import com.zjdiepu.www.ibiz.zpp.XinHeHuiApiService;
import com.zjdiepu.www.model.partner.dto.ThYwBusiness;
import com.zjdiepu.www.response.BaseResponse;
import com.zjdiepu.www.response.BooleanResponse;
import com.zjdiepu.www.security.server.convert.MyJson;
import com.zjdiepu.www.security.server.core.Partners;
import com.zjdiepu.www.security.server.interceptor.PartnerProvider;

/**
 * 类描述:鑫合汇接口<br/> 
 * 项目名称:diepu-parent-partner <br/> 
 * 类名称:XinHeHuiController <br/>     
 * 创建人:xiongxiaotun <br/> 
 * 创建时间:2017年8月15日 下午5:56:32  <br/>   
 * 修改人:xiongxiaotun <br/> 
 * 修改时间: 2017年8月15日 下午5:56:32   <br/>  
 * 修改备注:   <br/> 
 * @version V1.0   <br/>
 */
@MyJson
@RestController
@RequestMapping(value="/", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
@Api(tags = { "鑫合汇接口" }, produces = "application/json;charset=UTF-8")
@PartnerProvider(partner = Partners.XIN_HE_HUI)
public class XinHeHuiController extends BaseController {
	
	@Resource
	private IThYwBusinessService thYwBusinessService;
	@Resource
	private IThYwBusinessRequestService thYwBusinessRequestService;
	@Resource
	private XinHeHuiApiService xinHeHuiApiService;
	@Resource
	private IThLendingService thLendingService;
	@Resource
	private IThLogsService thLogsService;
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: noticeStatus <br/>
	 * @Description: 通知当前状态 <br/>
	 * @param noticeStatusRequest
	 * @return
	 * @version V1.0 <br/>
	 */
	@ApiOperation(value = "通知当前状态", response = BaseResponse.class, httpMethod = "POST")
	@RequestMapping("noticeStatus")
	public BaseResponse<BooleanResponse> noticeStatus(
			@Validated @RequestBody NoticeStatusRequest noticeStatusRequest) {
		String xe_apply_no = noticeStatusRequest.getXe_apply_no();
		Integer xe_apply_state = noticeStatusRequest.getXe_apply_state();
		thLogsService.saveOrUpdateLog(null, null, "xhh", xe_apply_no, JSON.toJSONString(noticeStatusRequest), 
				null, null, ThirdLogEventStatus.SUCCESS, "通知当前状态", null,
					"noticeStatus", "通知当前状态");
//		初审完成通知	1
//		终审完成通知	2
//		风险审批完成通知	3
//		募集完成通知	4
//		还款计划完成通知	5
//		放款完成通知	6
//		代偿完成通知	8
		ThYwBusiness thYwBusiness = new ThYwBusiness();
		thYwBusiness.setThirdId(xe_apply_no);
		thYwBusiness.setDpStatus(DpStatus.NORMAL.getCode());
		thYwBusiness = thYwBusinessService.selectRefined(thYwBusiness);
		if(null == thYwBusiness) {
			return BaseResponse.failWithBoolean(PartnerErrorCode.TH_BUSINESS_MISSING.getCode(), PartnerErrorCode.TH_BUSINESS_MISSING.getName());
		}
		String busiId = thYwBusiness.getBusiId();
		if(StringUtils.isBlank(busiId)) {
			return BaseResponse.failWithBoolean(PartnerErrorCode.BUSI_ID_MISSING.getCode(), PartnerErrorCode.BUSI_ID_MISSING.getName());	
		}
//		1:初审查询 2:终审查询 3:风险审批查询
		String busiType = Partners.XIN_HE_HUI.getCode();
		boolean result = true;
		if(XeApplyState.CHU_SHEN_WAN_CHENG.getCode() == xe_apply_state) {
			result = thYwBusinessRequestService.saveQueryXinHeHuiStatus(busiId, 1, String.valueOf(XeApplyState.CHU_SHEN_WAN_CHENG.getCode()), XeApplyState.CHU_SHEN_WAN_CHENG.getName());
		} else if(XeApplyState.ZHONG_SHEN_WAN_CHEN.getCode() == xe_apply_state) {
			result = thYwBusinessRequestService.saveQueryXinHeHuiStatus(busiId, 2, String.valueOf(XeApplyState.ZHONG_SHEN_WAN_CHEN.getCode()), XeApplyState.ZHONG_SHEN_WAN_CHEN.getName());
		} else if(XeApplyState.FENG_KONG_SHEN_PI_WAN_CHENG.getCode() == xe_apply_state) {
			result = thYwBusinessRequestService.saveQueryXinHeHuiStatus(busiId, 3, String.valueOf(XeApplyState.FENG_KONG_SHEN_PI_WAN_CHENG.getCode()), XeApplyState.FENG_KONG_SHEN_PI_WAN_CHENG.getName());
		} else if(XeApplyState.MU_JI_WAN_CHENG.getCode() == xe_apply_state) {
			String diepuMuJiOrJihuaApi = xinHeHuiApiService.diepuMuJiOrJihuaApi(String.valueOf(XeApplyState.MU_JI_WAN_CHENG.getCode()), XeApplyState.MU_JI_WAN_CHENG.getName(), busiId,busiType);
			result = StringUtils.isNotBlank(diepuMuJiOrJihuaApi);
		} else if(XeApplyState.HUAN_KUAN_JI_HUA_WAN_CHEGN.getCode() == xe_apply_state) {
			String diepuMuJiOrJihuaApi = xinHeHuiApiService.diepuMuJiOrJihuaApi(String.valueOf(XeApplyState.HUAN_KUAN_JI_HUA_WAN_CHEGN.getCode()), XeApplyState.HUAN_KUAN_JI_HUA_WAN_CHEGN.getName(), busiId, busiType);
			result = StringUtils.isNotBlank(diepuMuJiOrJihuaApi);
		} else if(XeApplyState.FANG_KUAN_WAN_CHENG.getCode() == xe_apply_state) {
			result = thLendingService.saveApplyXinHeHuiLending(busiId, String.valueOf(XeApplyState.FANG_KUAN_WAN_CHENG.getCode()), XeApplyState.FANG_KUAN_WAN_CHENG.getName());
		} else if(XeApplyState.DAI_CHANG_WAN_CHENG.getCode() == xe_apply_state) {
			result = thYwBusinessRequestService.saveQueryRepay(busiId, String.valueOf(XeApplyState.DAI_CHANG_WAN_CHENG.getCode()), XeApplyState.DAI_CHANG_WAN_CHENG.getName());
		}
		
		return BaseResponse.successWithBoolean(result);
	}
	
}
